package com.apes.crm.customerCar.service;

import com.apes.crm.car.model.CarBase;
import com.apes.crm.customerCar.model.CustomerCar;
import com.apes.crm.customerCar.repository.CustomerCarRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.service.PartyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service("customerCarService")
public class CustomerCarService extends DomainService{
    @Autowired
    public CustomerCarRepository customerCarRepository;

    @Autowired
    PartyService partyService;


    /*
    * 定义客户车辆弹窗 增加客户编码
    * */
    public Page<CustomerCar> findAllForUi(SimpleRequest simpleRequest) {
        Page<CustomerCar> customerCarPage = simpleRequest.getO(Page.class);

        customerCarPage.getContent().forEach(
                (customerCar) -> {
                    Party party = customerCar.getParty();
                    party.setCustomerCode(partyService.findRoleCodeByRole(party,"Customer"));
                }
        );
        return customerCarPage;
    }

    public CustomerCar findCarByVipInfoAndCarID(Party party, CarBase casebase){
        return customerCarRepository.findFirstByPartyEqualsAndCarEquals(party,casebase);
    }

}
